package com.wejoy.video.middle.station.admin.controller.res;

import com.alibaba.fastjson.JSONObject;
import com.wejoy.video.middle.station.admin.controller.BaseController;
import com.wejoy.video.middle.station.admin.util.CurrentUserUtils;
import com.wejoy.video.middle.station.data.annotation.RoleCheck;
import com.wejoy.video.middle.station.data.domain.PageResult;
import com.wejoy.video.middle.station.data.domain.res.VideoResLib;
import com.wejoy.video.middle.station.data.domain.system.SysUser;
import com.wejoy.video.middle.station.data.service.file.IFileService;
import com.wejoy.video.middle.station.data.service.res.IVideoResLibService;
import com.wejoy.video.middle.station.data.util.WJAssert;
import com.wejoy.video.middle.station.data.vo.RespJSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 视频资源库
 *
 * @author Administrator
 * @date 2021年10月25日 17:52:36
 */
@RestController
@RequestMapping("/multiMediaLib/videoResLib")
public class VideoResLibController extends BaseController {

    @Autowired
    private IVideoResLibService videoResLibService;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private IFileService fileService;

    private static final String VIDEO_RESLIB_MAP = "videoReslibMap";

    @Value("${qiniu.prefix}")
    private String qiniuVideoPrefix;

    /**
     * 保存或更新
     *
     * @param videoResLibReq videoResLibReq
     * @return RespJSON
     */
    @PostMapping("/save")
    @RoleCheck(name = "photoResLib_edit,photoResLib_add")
    public RespJSON save(@RequestBody VideoResLib videoResLibReq, @SessionAttribute(CurrentUserUtils.SYSTEM_USER) SysUser loginSysUser) {
        videoResLibService.save(videoResLibReq, loginSysUser.getName());
        return RespJSON.success();
    }

    /**
     * 批量保存或更新
     *
     * @param videoReslibMap
     * @return RespJSON
     */
    @RequestMapping("/batchSave")
    @RoleCheck(name = "photoResLib_edit,photoResLib_add")
    public RespJSON batchSave(@RequestBody Map<String, List<VideoResLib>> videoReslibMap, @SessionAttribute(CurrentUserUtils.SYSTEM_USER) SysUser loginSysUser) {
        videoReslibMap.get(VIDEO_RESLIB_MAP).forEach(videoResLib -> {
            videoResLibService.save(videoResLib, loginSysUser.getName());
        });
        return RespJSON.success();
    }

    @PostMapping("/delete")
    @RoleCheck(name = "photoResLib_delete")
    public RespJSON delete(Long id, @SessionAttribute(CurrentUserUtils.SYSTEM_USER) SysUser loginSysUser) {
        WJAssert.notNull(id, "ID 不合法");
        videoResLibService.delete(id, loginSysUser.getUserName());
        return RespJSON.success();
    }

    /**
     * 获得上传token
     * param resVideoLibReq.videoPath 完整的文件路径, 注意这个路径前面没有/
     *
     * @return
     */
    @RequestMapping("/getUploadToken")
    @ResponseBody
    public RespJSON uploadTokenByType(@RequestBody VideoResLib resVideoLibReq) {
        WJAssert.notNull(resVideoLibReq.getPath(), "视频路径非法");
        String uploadToken = fileService.getUploadToken(resVideoLibReq.getPath());
        return RespJSON.success(uploadToken);
    }


    /**
     * 分页查询
     *
     * @param videoResLibReq videoResLibReq
     * @return RespJSON
     */
    @PostMapping("/getPageList")
    @RoleCheck(name = "photoResLib_list")
    public RespJSON getPageList(@RequestBody VideoResLib videoResLibReq) {
        int count = videoResLibService.getCount(videoResLibReq);
        List<VideoResLib> list = new ArrayList<>();
        if (count > 0) {
            list = videoResLibService.getPageList(videoResLibReq);
            list.parallelStream().forEach(item -> {
                String[] urlSplit = item.getPath().split("\\.");
                String url = qiniuVideoPrefix + urlSplit[0] + "@hls.m3u8?avinfo";
                try {
                    restTemplate.getForObject(url, JSONObject.class);
                    item.setPlayHls(url.replace("?avinfo", ""));
                } catch (RestClientException e) {
                    logger.debug("转码视频未生成");
                }
            });
        }
        PageResult result = new PageResult(videoResLibReq.getPageNo(), videoResLibReq.getPageSize(), count, list);
        return RespJSON.success(result);
    }

    /**
     * 主键查询
     *
     * @param id id
     * @return RespJSON
     */
    @RequestMapping("/getOneById")
    @RoleCheck(name = "photoResLib_list")
    public RespJSON getOneById(Long id) {
        return RespJSON.success(videoResLibService.getOneById(id));
    }

    /**
     * 审核
     *
     * @param id id
     * @return RespJSON
     */
    @PostMapping("/audit")
    @ResponseBody
    public RespJSON audit(Long id, @SessionAttribute(CurrentUserUtils.SYSTEM_USER) SysUser loginSysUser) {
        WJAssert.notNull(id, "ID 不合法");
        videoResLibService.audit(id, loginSysUser.getUserName());
        return RespJSON.success();
    }

    /**
     * 取消审核
     *
     * @param id id
     * @return RespJSON
     */
    @PostMapping("/unaudit")
    @ResponseBody
    public RespJSON unaudit(Long id, @SessionAttribute(CurrentUserUtils.SYSTEM_USER) SysUser loginSysUser) {
        WJAssert.notNull(id, "ID 不合法");
        videoResLibService.unaudit(id, loginSysUser.getUserName());
        return RespJSON.success();
    }

}
